package com.yoo.ons.util

import android.content.Context
import android.net.Uri
import com.google.firebase.ktx.Firebase
import com.google.firebase.ktx.initialize
import com.google.firebase.storage.FirebaseStorage
import java.io.File

object UploadPhotoUtil {

    fun uploadFile(context: Context,path: String, mediaType: String, onLister: OnListener){
        val firebaseApp = Firebase.initialize(context)
        val storageRef = FirebaseStorage.getInstance(firebaseApp!!).reference

        val file = Uri.fromFile(File(path))
        val riversRef = storageRef.child("ons/images/${file.lastPathSegment}")
        val uploadTask = riversRef.putFile(file)


        uploadTask.addOnFailureListener {
            onLister.fail()
        }.addOnSuccessListener { taskSnapshot ->
            onLister.onSuccess(taskSnapshot.storage.path,"")
        }

    }
    fun uploadPhoto(context: Context,path: String, imageCode: String, onLister: OnListener){
        val firebaseApp = Firebase.initialize(context)
        val storageRef = FirebaseStorage.getInstance(firebaseApp!!).reference

        val file = Uri.fromFile(File(path))
        val riversRef = storageRef.child("ons/images/${file.lastPathSegment}")
        val uploadTask = riversRef.putFile(file)

        uploadTask.addOnFailureListener {
            onLister.fail()
        }.addOnSuccessListener { taskSnapshot ->
            onLister.onSuccess(taskSnapshot.storage.path,imageCode)
        }

    }
    interface OnListener{
        fun onSuccess(successPath:String,imageCode:String)
        fun fail()
    }

}